package com.lx.zy.project.system.controller;

import com.lx.zy.common.constant.UserConstants;
import com.lx.zy.common.utils.SecurityUtils;
import com.lx.zy.framework.aspectj.lang.annotation.Log;
import com.lx.zy.framework.aspectj.lang.enums.BusinessType;
import com.lx.zy.framework.web.controller.BaseController;
import com.lx.zy.framework.web.domain.AjaxResult;
import com.lx.zy.framework.web.domain.BaseEntity;
import com.lx.zy.project.system.domain.SysDept;
import com.lx.zy.project.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: zy-vue
 * @description: 部门信息 web层
 * @author: Lx
 * @create: 2019-11-04 01:55
 **/
@RestController
@RequestMapping("/system/dept")
public class SysDeptController extends BaseController
{
    @Autowired
    private ISysDeptService deptService;

    @PreAuthorize("@ps.hasPermi('system:dept:list')")
    @GetMapping("/list")
    public AjaxResult list(SysDept dept)
    {
        List<SysDept> depts = deptService.selectDeptList(dept);
        return AjaxResult.success(deptService.buildDeptTree(depts));
    }

    @PreAuthorize("@ps.hasPermi('system:dept:query')")
    @GetMapping("/treeselect")
    public AjaxResult treeSelect(SysDept dept)
    {
        List<SysDept> depts = deptService.selectDeptList(dept);
        return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
    }

    @PreAuthorize("@ps.hasPermi('system:dept:add')")
    @Log(title = "部门管理", businessType = BusinessType.INSERT)
    @PutMapping
    public AjaxResult add(@RequestBody SysDept dept)
    {
        if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
        {
            return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }
        dept.setCreateBy(SecurityUtils.getUserName());
        return toAjax(deptService.insertDept(dept));
    }
}
